#include "storage.h"

Storage::Storage(const char *name) : Resource(name) {

	this->resource = SG_newHost(name, 1, SG_SEQUENTIAL_IN_ORDER, NULL, 0, 0, NULL,0,0,NULL);
	this->datas = NULL;
	this->numberOfDatas = 0;

}

void Storage::addData(const char *name, long double size, int type) {
	this->numberOfDatas++;

	this->datas = (DataElement **) realloc(this->datas, this->numberOfDatas*sizeof(DataElement *));

	this->datas[numberOfDatas - 1] = new DataElement(name, size, type);
}

DataElement *Storage::getDataElementByIndex(int index) {
	return this->datas[index];
}

DataElement *Storage::getDataElementByName(const char *name) {
	DataElement *data=this->existDataElement(name);
	if (data==NULL) throw std::runtime_error("Data does not exist in this storage");
	return data;
}

DataElement *Storage::existDataElement(const char *name) {
	for (int n=0; n<this->numberOfDatas; n++) {
		if (!strcmp(name, this->getDataElementByIndex(n)->getName())) {
			return this->getDataElementByIndex(n);
		}
	}
	return NULL;
}

int Storage::getNumberOfDatas() {
	return this->numberOfDatas;
}

Storage::~Storage() {

	for (int n = 0; n < this->numberOfDatas; n++) {
			if (this->datas[n]) delete this->datas[n];		
	}
	
}
